﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BE.Abastos;

namespace BC.Abastos
{
    public class MeasuresBC
    {
        public Measures Create(Measures item)
        {
            try
            {
                Measures result = new Measures();
                if (item == null)
                {
                    throw new BE.BusinessException("La medida tiene un formato inválido");
                }
                else if (string.IsNullOrEmpty(item.sMeasureName))
                {
                    throw new BE.BusinessException("El nombre de la medida es requerido.");
                }
                else if ((from obj in new Measures() { iMeasureId = -1, sMeasureName = string.Empty }.Select()
                          where obj.sMeasureName == item.sMeasureName
                          select obj.iMeasureId).Count() > 0)
                {
                    throw new BE.BusinessException("Ya existe una medida con ese nombre.");
                }
                else
                {
                    result = item.Create();
                }
                return result;
            }
            catch (BE.AppException ex)
            {
                throw new BE.AppException("MeasuresBC - Create - ", ex);
            }
            catch(BE.BusinessException ex)
            {
                throw new BE.BusinessException("MeasuresBC - Create - ", ex);
            }
            catch(Exception ex)
            {
                throw new Exception("MeasuresBC - Create - ", ex);
            }
        }

        public List<Measures> Select(Measures item)
        {
            try
            {
                List<Measures> result = new List<Measures>();
                result = item.Select();
                return result;
            }
            catch (BE.AppException ex)
            {
                throw new BE.AppException("MeasuresBC - Select - ", ex);
            }
            catch (BE.BusinessException ex)
            {
                throw new BE.BusinessException("MeasuresBC - Select - ", ex);
            }
            catch (Exception ex)
            {
                throw new Exception("MeasuresBC - Select - ", ex);
            }
        }

    }
}
